<template>
    <div>
        <PageTitle title="系统配置"></PageTitle>
        <Form ref="form" :model="form" :rules="rules" :label-width="120">
            <FormItem label="基础保证金" prop="base_deposit">
                <InputNumber v-model="form.base_deposit" style="width:200px" :min="0" :precision="2" />
            </FormItem>
            <FormItem label="商家违约手续费" prop="partner_breach_fee">
                <InputNumber v-model="form.partner_breach_fee" style="width:200px" :min="0" :max="1000" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" />
            </FormItem>
            <FormItem label="商家缺票手续费" prop="partner_lack_fee">
                <InputNumber v-model="form.partner_lack_fee" style="width:200px" :min="0" :max="100" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" />
            </FormItem>
            <FormItem label="商家结算费率" prop="partner_settle_rate">
                <InputNumber v-model="form.partner_settle_rate" style="width:200px" :min="0" :max="100" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" />
            </FormItem>
            <FormItem label="用户取消手续费" prop="user_cancel_fee">
                <InputNumber v-model="form.user_cancel_fee" style="width:200px" :min="0" :max="100" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" />
            </FormItem>
            <FormItem label="商家确认分钟数" prop="order_auto_confirm">
                <InputNumber v-model="form.order_auto_confirm" style="width:200px" :min="0" :max="100" />
            </FormItem>
            <FormItem label="演出结束+h结算" prop="order_settle_add_hours">
                <InputNumber v-model="form.order_settle_add_hours" style="width:200px" :min="0" :max="100" />
            </FormItem>
            <FormItem>
                <Button type="primary" :loading="submiting" @click="handleSubmit">提交</Button>
            </FormItem>
        </Form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            submiting: false,
            form: {
                base_deposit: 0,
                partner_breach_fee: 0,
                partner_lack_fee: 0,
                partner_settle_rate: 0,
                user_cancel_fee: 0,
                order_auto_confirm: 0,
                order_settle_add_hours: 0,
            },
            rules: {
                base_deposit: [
                    {required: true, type: "number", message: '请输入基础保证金', trigger: 'blur'}
                ],
                partner_breach_fee: [
                    {required: true, type: "number", message: '请输入手续费', trigger: 'blur'}
                ],
                partner_lack_fee: [
                    {required: true, type: "number", message: '请输入手续费', trigger: 'blur'}
                ],
                partner_settle_rate: [
                    {required: true, type: "number", message: '请输入商家结算费率', trigger: 'blur'}
                ],
                user_cancel_fee: [
                    {required: true, type: "number", message: '请输入手续费', trigger: 'blur'}
                ],
                order_auto_confirm: [
                    {required: true, type: "number", message: '请输商家确认分钟数', trigger: 'blur'}
                ],
                order_settle_add_hours: [
                    {required: true, type: "number", message: '请输演出结束+h结算', trigger: 'blur'}
                ]
            }
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.$ajax.get('common/config/get').then(res => {
                this.form.base_deposit = Number(res.base_deposit);
                this.form.partner_breach_fee = Number(res.partner_breach_fee);
                this.form.partner_lack_fee = Number(res.partner_lack_fee);
                this.form.partner_settle_rate = Number(res.partner_settle_rate);
                this.form.user_cancel_fee = Number(res.user_cancel_fee);
                this.form.order_auto_confirm = Number(res.order_auto_confirm);
                this.form.order_settle_add_hours = Number(res.order_settle_add_hours);
            }).catch(err => {
                this.$Message.error(err.message || '加载失败');
            })
        },
        handleSubmit() {
            if(this.submiting) return false;
            this.$refs.form.validate((valid) => {
                if(valid) {
                    this.submiting = true;
                    this.$ajax.post('/common/config/set', this.form).then(res => {
                        this.$Message.success('配置成功');
                    }).catch(err => {
                        this.submiting = false;
                        this.$Message.error(err.message || '提交失败');
                    })
                }
            })
        }
    }
}
</script>